#!/bin/bash

#######################################################
#
# evert.lammerts@sara.nl
#
# USAGE: ./topos [ command ] [ arguments ]
#
# where the combination command and arguments is one of:
# * uniquePoolName
# * nextToken [POOLNAME]
# * nextTokenWithLock [POOLNAME] [LOCKDURATION]
# * createTokensFromLinesInFile [POOLNAME] [TXTFILE]
# * refreshLock [POOLNAME] [LOCKNAME] [LOCKDURATION]
# * getToken [POOLNAME] [TOKENNAME]
# * deleteToken [POOLNAME] [TOKENNAME]
#
#######################################################

TOPOS_URL="https://topos.grid.sara.nl/4.1/"
CURL="curl --silent --fail --insecure"

EXIT_NOCOMMAND=-1
EXIT_MISSINGPARAM=1
EXIT_FILENOTFOUND=2
EXIT_CURLERROR=3

function uniquePoolName {
  URL=$( ${CURL} --header 'Accept: text/plain' "${TOPOS_URL}newPool" )
  if [ "$?" != "0" ]; then
    exit $EXIT_CURLERROR
  fi
  URL=( $( echo $URL | tr '/' ' ' ) )
  echo ${URL[$[ ${#URL[@]}-1 ]]}
}

function createTokensFromLinesInFile {
  local poolName="$1"
  local fileName="$2"
  if [ -z $fileName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ -z $poolName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ ! -e $fileName ]; then
    exit $EXIT_FILENOTFOUND
  fi
  $CURL --data "@${fileName}" --header 'Content-Type: text/plain' "${TOPOS_URL}pools/${poolName}/tokens/"
  if [ "$?" != "0" ]; then
    exit $EXIT_CURLERROR
  fi
}

function nextToken {
  local poolName="$1"
  if [ -z $poolName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  TOKEN=$( ${CURL} --header 'Accept: text/plain' "${TOPOS_URL}pools/${poolName}/nextToken" )
  if [ "$?" != "0" ]; then
    exit $EXIT_CURLERROR
  fi
  echo ${TOKEN} | tr '/' ' ' | awk '{print $NF}'
}

function nextTokenWithLock {
  local poolName="$1"
  local timeout="$2"
  if [ -z $poolName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ -z $timeout ]; then
    exit $EXIT_MISSINGPARAM
  fi
  local headerFile="$( tempfile )"
  TOKEN=$( ${CURL} --header 'Accept: text/plain' --dump-header ${headerFile} "${TOPOS_URL}pools/${poolName}/nextToken?timeout=${timeout}" )
  if [ "$?" != "0" ]; then
    exit $EXIT_CURLERROR
  fi
  echo ${TOKEN} | tr '/' ' ' | awk '{print $NF}'
  LOCK=$( cat ${headerFile} | grep X-Topos-LockURL )
  echo ${LOCK#* } | tr '/' ' ' | awk '{print $NF}'
  rm $headerFile
}

function refreshLock {
  local poolName="$1"
  local lockName="$2"
  local timeout="$3"
  if [ -z $poolName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ -z $lockName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ -z $timeout ]; then
    exit $EXIT_MISSINGPARAM
  fi
  ${CURL} --request HEAD "${TOPOS_URL}pools/${poolName}/locks/${lockName}?timeout=${timeout}"
  if [ "$?" != "0" ]; then
    exit $EXIT_CURLERROR
  fi
}

function deleteLock {
  local poolName="$1"
  local lockName="$2"
  if [ -z $poolName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ -z $lockName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  ${CURL} --request DELETE "${TOPOS_URL}pools/${poolName}/locks/${lockName}"
  if [ "$?" != "0" ]; then
    exit $EXIT_CURLERROR
  fi
}

function getToken {
  local poolName="$1"
  local tokenName="$2"
  if [ -z $poolName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ -z $tokenName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  ${CURL} --header 'Accept: text/plain' "${TOPOS_URL}pools/${poolName}/tokens/${tokenName}"
}

function deleteToken {
  local poolName="$1"
  local tokenName="$2"
  if [ -z $poolName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  if [ -z $tokenName ]; then
    exit $EXIT_MISSINGPARAM
  fi
  ${CURL} --request DELETE --header 'Accept: text/plain' "${TOPOS_URL}pools/${poolName}/tokens/${tokenName}"
}

COMMAND="$1"
if [ -z $COMMAND ]; then
  exit $EXIT_NOCOMMAND
fi
shift
$COMMAND $1 $2 $3 $4

#uniquePoolName
#nextToken 364cefe6446759007623cc32
#nextTokenWithLock 364cefe6446759007623cc32 120
#createTokensFromLinesInFile 364cefe6446759007623cc32 fractals.c
#refreshLock 364cefe6446759007623cc32 dc16835c-3cbf-11df-9264-001d0911e44a 240
#getToken 364cefe6446759007623cc32 10291138
#deleteToken 364cefe6446759007623cc32 10291137
